package com.paomi.client.fragment.card;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseViewHolder;
import com.paomi.client.GlideApp;
import com.paomi.client.R;
import com.paomi.client.activity.detail.DetailTicketActivity;
import com.paomi.client.activity.main.MainActivity;
import com.paomi.client.activity.person.model.MyCardBean;
import com.paomi.client.base.BaseLCEFragment;
import com.paomi.client.bean.UserInfo;
import com.paomi.client.fragment.card.presenter.CardNotUsedPre;
import com.paomi.client.utils.UIUtils;
import com.paomi.client.utils.Util;

import java.text.SimpleDateFormat;
import java.util.List;

import butterknife.BindView;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;

/**
 * 去使用
 */
public class CardNotUsedFragment extends BaseLCEFragment<MyCardBean.Data, CardNotUsedPre> {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.ptr_refresh)
    PtrClassicFrameLayout ptrRefresh;
    private int page=1;

    @NonNull
    @Override
    protected int getFragmentLayoutId() {
        return R.layout.layout_only_rec;
    }

    @NonNull
    @Override
    protected RecyclerView createRecycler() {
        return recyclerView;
    }

    @Override
    protected int getItemLayout() {
        return R.layout.item_my_card;
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        createRecycler().setPadding(0,0,0, UIUtils.dp2px(getActivity(),80f));
        setPtrFrameLayout(ptrRefresh);
        getPresenter().getLoadData(getActivity(),page);
    }

    @Override
    protected void initEmpty(TextView empty) {
        empty.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.img_k_kaquan, 0, 0);
        empty.setText(getResources().getString(R.string.card_list_null));

    }

    @Override
    protected void convert(BaseViewHolder helper, MyCardBean.Data item) {
        RelativeLayout rl_ls = helper.itemView.findViewById(R.id.rl_ls);
        RelativeLayout rl_lx = helper.itemView.findViewById(R.id.rl_lx);
        LinearLayout rl_rs = helper.itemView.findViewById(R.id.rl_rs);
        RelativeLayout rl_rx = helper.itemView.findViewById(R.id.rl_rx);
        int width = Util.getDisplay(getActivity()).widthPixels - Util.dip2px(getActivity(), 30);
        Util.setWidthAndHeight(rl_ls, (int) (width * 0.27), -1);
        Util.setWidthAndHeight(rl_lx, (int) (width * 0.27), -1);
        Util.setWidthAndHeight(rl_rs, (int) (width * 0.73), -1);
        Util.setWidthAndHeight(rl_rx, (int) (width * 0.73), -1);
        ImageView iv = helper.getView(R.id.iv_img);
        helper.setTextColor(R.id.click, getResources().getColor(R.color.colorFF3B30));
        helper.setBackgroundRes(R.id.click, R.drawable.btn_red_out);
        helper.setText(R.id.click, R.string.not_used);
        if (item.getType() == 1) {
            GlideApp.with(getActivity()).asBitmap().load(R.mipmap.img_card_you_y).into(iv);
        } else {
            GlideApp.with(getActivity()).asBitmap().load(R.mipmap.img_card_piao_y).into(iv);
        }
        helper.setText(R.id.tv_card_name, getResources().getString(R.string.card_name) + item.getName());
        helper.setText(R.id.tv_msg, "满" + item.getEnableAmount() + "使用");
        helper.setTextColor(R.id.tv_msg, getResources().getColor(R.color.color_fff3b4));
        if(item.getAssName()==null){
            helper.setText(R.id.tv_card_assname, "");
        }else{
            helper.setText(R.id.tv_card_assname, getResources().getString(R.string.card_assName) + item.getAssName());
        }
        helper.setText(R.id.tv_card_data, getResources().getString(R.string.card_data)
                + new SimpleDateFormat("yyyy-MM-dd").format(item.getExpireDdate() * 1000));

        helper.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(getActivity(), MainActivity.class);
                i.putExtra("id", 1);
                i.putExtra("mainId","1");
                startActivity(i);
            }
        });
    }

    @Override
    protected void onItemClick(View view, MyCardBean.Data item) {

    }

    @Override
    public CardNotUsedPre createPresenter() {
        return new CardNotUsedPre();
    }

    @Override
    public void setData(List<MyCardBean.Data> data, boolean pullToRefresh) {
        super.setData(data, pullToRefresh);
        if(getmAdapterWrapper()!=null){
            getmAdapterWrapper().setLoadItemVisibility(pullToRefresh);
            getmLoadMoreHelper().setLoadMoreFinish();
            getmAdapterWrapper().notifyDataSetChanged();
        }else{
            getAdapter().notifyDataSetChanged();
        }
    }
    @Override
    public void loadData(boolean pullToRefresh) {
        super.loadData(pullToRefresh);
        //下拉刷新
        page=1;
        getPresenter().getLoadData(getActivity(),page);
    }

    @Override
    public void onLoad() {
        super.onLoad();
        //加载更多
        page++;
        getPresenter().getLoadData(getActivity(),page);
    }
}
